<template>
  <!-- 应用导航详情抽屉 -->  
  <div class="app-appNav-detail">
    <el-drawer v-model="isShowDialog" size="80%" direction="ltr">
      <template #header>
        <h4>应用导航详情</h4>
      </template>
      <el-form ref="formRef" :model="formData" label-width="100px">          
        <el-row>        
          <el-col :span="12">          
            <el-form-item label="ID">{{ formData.id }}</el-form-item>          
          </el-col>        
          <el-col :span="12">          
            <el-form-item label="名称">{{ formData.name }}</el-form-item>          
          </el-col>        
          <el-col :span="12">
            <el-form-item label="图标">
              <el-image
                style="width: 150px; height: 150px"
                v-if="!proxy.isEmpty(formData.icon)"
                :src="proxy.getUpFileUrl(formData.icon)"
                fit="contain"></el-image>
            </el-form-item>
          </el-col>        
          <el-col :span="12">          
            <el-form-item label="描述">{{ formData.description }}</el-form-item>          
          </el-col>        
          <el-col :span="12">
            <el-form-item label="创建日期">{{ proxy.parseTime(formData.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</el-form-item>
          </el-col>        
          <el-col :span="12">          
          <el-form-item label="状态">{{ proxy.getOptionValue(formData.status, statusOptions,'value','label') }}</el-form-item>          
          </el-col>      
        </el-row>      
      </el-form>
    </el-drawer>
  </div>
</template>
<script lang="ts">
  import { reactive, toRefs, defineComponent,ref,unref,getCurrentInstance,computed } from 'vue';
  import {ElMessageBox, ElMessage, FormInstance,UploadProps} from 'element-plus';  
  import {
    listAppNav,
    getAppNav,
    delAppNav,
    addAppNav,
    updateAppNav,    
  } from "/@/api/app/appNav";  
  import {getToken} from "/@/utils/gfast"  
  import {
    AppNavTableColumns,
    AppNavInfoData,
    AppNavTableDataState,
    AppNavEditState
  } from "/@/views/app/appNav/list/component/model"
  export default defineComponent({
    name:"ApiV1AppAppNavDetail",    
    components:{      
    },    
    props:{      
      statusOptions:{
        type:Array,
        default:()=>[]
      },      
    },    
    setup(props,{emit}) {      
      const baseURL:string|undefined|boolean = import.meta.env.VITE_API_URL      
      const {proxy} = <any>getCurrentInstance()
      const formRef = ref<HTMLElement | null>(null);
      const menuRef = ref();      
      //图片上传地址
      const imageUrlIcon = ref('')
      //上传加载
      const upLoadingIcon = ref(false)      
      const state = reactive<AppNavEditState>({
        loading:false,
        isShowDialog: false,
        formData: {          
          id: undefined,          
          name: undefined,          
          icon: undefined,          
          description: undefined,          
          createdAt: undefined,          
          deletedAt: undefined,          
          status: false ,          
        },
        // 表单校验
        rules: {          
          id : [
              { required: true, message: "ID不能为空", trigger: "blur" }
          ],          
          name : [
              { required: true, message: "名称不能为空", trigger: "blur" }
          ],          
          status : [
              { required: true, message: "状态不能为空", trigger: "blur" }
          ],          
        }
      });
        // 打开弹窗
        const openDialog = (row?: AppNavInfoData) => {
          resetForm();
          if(row) {
            getAppNav(row.id!).then((res:any)=>{
              const data = res.data;              
              //单图地址赋值
              imageUrlIcon.value = data.icon ? proxy.getUpFileUrl(data.icon) : ''              
              state.formData = data;
            })
          }
          state.isShowDialog = true;
        };
        // 关闭弹窗
        const closeDialog = () => {
          state.isShowDialog = false;
        };
        // 取消
        const onCancel = () => {
          closeDialog();
        };
        const resetForm = ()=>{
          state.formData = {            
            id: undefined,            
            name: undefined,            
            icon: undefined,            
            description: undefined,            
            createdAt: undefined,            
            deletedAt: undefined,            
            status: false ,            
          }
        };        
        //单图上传图标
        const handleAvatarSuccessIcon:UploadProps['onSuccess'] = (res, file) => {
          if (res.code === 0) {
            imageUrlIcon.value = URL.createObjectURL(file.raw!)
            state.formData.icon = res.data.path
          } else {
            ElMessage.error(res.msg)
          }
          upLoadingIcon.value = false
        }
        const beforeAvatarUploadIcon = () => {
          upLoadingIcon.value = true
          return true
        }        
        const setUpData = () => {
          return { token: getToken() }
        }        
        return {
          proxy,
          openDialog,
          closeDialog,
          onCancel,
          menuRef,
          formRef,          
          //图片上传地址
          imageUrlIcon,
          //上传加载
          upLoadingIcon,
          handleAvatarSuccessIcon,
          beforeAvatarUploadIcon,          
          setUpData,          
          baseURL,          
          ...toRefs(state),
        };
      }
  })
</script>
<style scoped>  
  .app-appNav-detail :deep(.avatar-uploader .avatar) {
    width: 178px;
    height: 178px;
    display: block;
  }
  .app-appNav-detail :deep(.avatar-uploader .el-upload) {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }
  .app-appNav-detail :deep(.avatar-uploader .el-upload:hover) {
    border-color: var(--el-color-primary);
  }
  .app-appNav-detail :deep(.el-icon.avatar-uploader-icon) {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
  }  
  .app-appNav-detail :deep(.el-form-item--large .el-form-item__label){
    font-weight: bolder;
  }
  .pic-block{
    margin-right: 8px;
  }
  .file-block{
    width: 100%;
    border: 1px solid var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
    margin-bottom: 5px;
    padding: 3px 6px;
  }
  .ml-2{margin-right: 5px;}
</style>